package sist.controller;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import sist.bean.Person;
@Controller
@Component
public class ScottController {	
	
	@RequestMapping(value="mvc/first.do")
	public ModelAndView firstAction(){
		ModelAndView mv=new ModelAndView();
		mv.setViewName("/mvc/view");
		mv.addObject("view", "SpringView");
		return mv;
	}
	
	@RequestMapping(value="mvc/actionUrl.do",method=RequestMethod.GET)	
	public ModelAndView methodView(@ModelAttribute("command")Person person){
		ModelAndView mv=new ModelAndView();
		System.out.println(person);
		mv.setViewName("mvc/getView");
		return mv;
	}
	@RequestMapping(value="mvc/actionUrl.do",method=RequestMethod.POST)	
	public ModelAndView methodViewPost(@ModelAttribute("command")Person person){
		ModelAndView mv=new ModelAndView();
		System.out.println(person);
		mv.setViewName("mvc/postView");
		return mv;
	}
	@RequestMapping(value="mvc/param.do")
	public String paramTest(@RequestParam(value="ename",required=false)String ename){
		
		System.out.println(ename);
		return "mvc/param";
	}
	@RequestMapping(value="mvc/head.do")
	public void urlTest(){
		System.out.println("urlTest");
	}
	@RequestMapping(value="mvc/mapRetur.do",method=RequestMethod.GET)
	public Map<String, Object>returnMapSelect(@RequestParam(value="ename",required=false)String ename,@RequestParam(value="job",required=false)String job){
         HashMap<String,Object>map=new HashMap<String,Object>();
         map.put("ename", ename);
         map.put("job", job);
         return map;
	}
	@ModelAttribute("list")
	public List<Person> listProcess(){
		ArrayList<Person>listPerson=new ArrayList<Person>();
		listPerson.add(0,new Person("김재선"));
		listPerson.add(1,new Person("감자"));
		listPerson.add(2,new Person("우주"));
		return listPerson;
	}
	@RequestMapping("mvc/listSearch.do")
	public String modelViewSearch(){
		return "/mvc/listSearch";
	}
	
	
	@ModelAttribute("res")
	public String[] arrayView(){
		String str="java oracle jsp jquery struts";
		String[] res=str.split(" ");
		return res;
	}
	@RequestMapping("mvc/listString.do")
	public String modelView(){
		return "/mvc/list";
	}
	
	@RequestMapping(value="mvc/cookieCheck.do")
	public String cookieCheckMethod(@CookieValue(value="apple",required=true,defaultValue="0")String cookieName){
		if(cookieName.trim().equals("sist")){
			return "mvc/cookieSuccess";
		}else{
			return "mvc/cookieFail";
		}
	}
	
	@RequestMapping(value="mvc/cookieAdd.do")
	@ModelAttribute
	public void cookieAdd(Model map,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		/*System.out.println(request+" "+response+" "+session);*/
		map.addAttribute("hi","hiSpring");
		Cookie cookie=new Cookie("apple","sist");
		cookie.setMaxAge(15);
		response.addCookie(cookie);
	}
	
	@RequestMapping(value="mvc/pathparam/{userid}/**",method=RequestMethod.GET)
	public String creatInfo(@PathVariable("userid")String userId,ModelMap map){
		System.out.println(userId);
		map.addAttribute("userId",userId);
		return "mvc/pathView";
	}
	
}